package com.tcs.test.http.jsoup;

import com.tcs.model.teachertest.SelectBean;
import com.tcs.model.teachertest.TopicBean;
import com.tcs.util.uuid.UUIDUtil;
import org.apache.commons.collections.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.collections.Lists;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class MiniJsoupTc {

    private final Logger logger = LoggerFactory.getLogger(MiniJsoupTc.class);

    private int id = 0;

    private final int yeraSubjectId = 5;        // 年度ID

    @Test
    public void main() {
        Document doc = null;
        Elements elements = null;
        List<TopicBean> topicList = Lists.newArrayList();
        try {
            doc = Jsoup.parse(this.getFile(), "UTF-8");
            elements = doc.getElementsByClass("introwz");
            Element tempEl = elements.get(0);
            Elements tempElements = tempEl.getElementsByTag("p");
            Element tempEle = null;
            TopicBean topicBean = null;
            List<SelectBean> selectLIst = Lists.newArrayList();
            int index = 3, size = tempElements.size() , loopIndex = 0, max = 7 , orderIndex = 0 , orderTopicIndex = 0;
            while ((tempEle = tempElements.get(index)) != null) {
                index++;
                ++loopIndex;
                if (tempEle.html().contains("材料分析题")) {break;}
                if (loopIndex == 1) {
                    topicBean = new TopicBean();
                    topicBean.setOrderIndex(++orderTopicIndex);
                    topicBean.setId(UUIDUtil.getUUID());
                    topicBean.setTitle(tempEle.text());
                    topicBean.setYearSubjectId(yeraSubjectId);          // 年度题目类型
                    topicBean.setTopicTypeId(1);            // 题目类型
//                    logger.info(" title : {} " , tempEle.text());
                }
                if (loopIndex > 1 && loopIndex <= max - 1) {
//                    logger.info(" content : {} " , tempEle.html());
                    SelectBean selectBean = new SelectBean();
                    selectBean.setId(UUIDUtil.getUUID());
                    selectBean.setOrderIndex(++orderIndex);
                    selectBean.setTitle(tempEle.text());
                    selectBean.setDaiMa(daiMaArray[orderIndex]);
                    selectLIst.add(selectBean);
                }
                if (loopIndex == max) {
                    topicBean.setDaan(tempEle.text());
                    List<SelectBean> tempSelectList = Lists.newArrayList();
                    tempSelectList.addAll(selectLIst);
                    topicBean.setSelectList(tempSelectList);
                    topicList.add(topicBean);
//                    logger.info(" footer : {} " , tempEle.text());
                    loopIndex = 0;
                    orderIndex = 0;
                    setId();
                    selectLIst.clear();
                }
                if (index >= size) {break;}
            }
            conventSQL(topicList);
        } catch (IOException e) {
            logger.error("", e);
        } finally {
            System.exit(0);
        }
    }

    private StringBuffer sqlTopic = new StringBuffer("INSERT INTO topic_t (ID,topic_type_id,year_subject_id,title,daAn,tishi, order_index ,create_date) VALUES ");

    private StringBuffer sqlSelect = new StringBuffer("INSERT INTO select_t (ID,title,order_index,daiMa,create_date) VALUES ");

    private StringBuffer sqlTopicSelect = new StringBuffer("INSERT INTO topic_select_t (ID,topic_id,select_id,create_date) VALUES ");

    private void conventSQL(List<TopicBean> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            for (TopicBean topicBean : list) {
                sqlTopic.append("(")
                        .append("'").append(topicBean.getId()).append("',")
                        .append(topicBean.getTopicTypeId()).append(",")
                        .append(topicBean.getYearSubjectId()).append(",")
                        .append("'").append(topicBean.getTitle()).append("',")
                        .append("'").append(topicBean.getDaan()).append("',")
                        .append("'").append(topicBean.getTiShi()).append("',")
                        .append(topicBean.getOrderIndex()).append(",")
                        .append(topicBean.getCreateDate()).append("), ");
                List<SelectBean> selectList = topicBean.getSelectList();
                for (SelectBean selectBean : selectList) {
                    sqlSelect.append("(")
                            .append("'").append(selectBean.getId()).append("',")
                            .append("'").append(selectBean.getTitle()).append("',")
                            .append(selectBean.getOrderIndex()).append(",")
                            .append("'").append(selectBean.getDaiMa()).append("',")
                            .append(selectBean.getCreateDate()).append("), ");
                    sqlTopicSelect.append("(")
                            .append("'").append(UUIDUtil.getUUID()).append("',")
                            .append("'").append(topicBean.getId()).append("',")
                            .append("'").append(selectBean.getId()).append("',")
                            .append(selectBean.getCreateDate()).append("), ");
                }
            }
        }
        logger.info(" \n topicSQL : \n {} \n selectSQL : \n {} \n sqlTopicSelect: \n {} " , sqlTopic , sqlSelect , sqlTopicSelect);
    }

    private void xi(Element element) {
        if (element.html().contains("src")) {
        }
    }

    final String [] daiMaArray = new String[]{"A","A","B","C","D"};

    private int getId() {
        return  id++;
    }

    private void setId () {
        id = 0;
    }

    private File getFile() {
        File file = null;
        try {
            String filePath = "G:\\eclipse-workspace\\teacher-course-system\\teacher-course-system-app-parent\\teacher-course-system-test\\src\\test\\resources\\config\\demo.txt";
//            file = new ClassPathResource("config/demo.txt").getFile();
            file =  new File(filePath);
            logger.info("file : {} " , file.getPath());
        } catch (Exception e) {
            logger.error("", e);
        }
        return file;
    }

    @Test
    public void getUUID() {
        logger.info(" {} " , UUIDUtil.getUUID());
    }

    /**
     * 解析字符串
     */
    public void parseStr() {

    }
}
